package com.ogawa.chair7808.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.ogawa.base.Constant.MqttConstant;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.StringUtils;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Device7808StateBean;
import com.ogawa.chair7808.evenbus.Event;
import com.ogawa.chair7808.manager.DataManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/4/8.
 */
public class LayoutBody7506 extends FrameLayout   {
    private ImageView mIvAirShoulder;//肩部气囊
    private ImageView mIvAirArm;//小手臂气囊
    private ImageView mIvAirSeat;//座位气囊
    private ImageView mIvAirCalf;//小腿气囊
    private ImageView mIvAirAnkle;//腿部气囊（脚踝跟腿部一致）
    private ImageView mIvAirFoot;//腿部气囊

    private ImageView mIvHotBack;//背部加热
//    private ImageView mIvHotSeat;//座位加热
    private ImageView mIvHotCalf;//小腿加热

    private ImageView mIvFootRollOne;
    private ImageView mIvFootRollTwo;

    private ImageView mIvKnead;//揉捏
    private ImageView mIvZhiya;//指压
    private ImageView mIvPaida;//拍打
    private ImageView mIvMassage;//推拿
    private ImageView mIvRDS;//瑞典
    private ImageView mIvKnock;//敲击
    private ImageView mBodyPoint1;
    private Context mContext;
    private String mData;
    private List<ImageView> mPointViews = new ArrayList<>();

    private String mDataFoot;
    private String mDataHot;

    public static final int KNEAD_ON = 1;
    public static final int KNEAD_OFF = 2;
    private List<ImageView> mAnimatorImage = new ArrayList<>();
    public LayoutBody7506(Context context) {
        super(context);
    }

    public LayoutBody7506(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBodyPoint1 = findViewById(R.id.body_point1);
        mIvFootRollOne = findViewById(R.id.foot_roll1);
        mIvFootRollTwo = findViewById(R.id.foot_roll2);
        mIvAirShoulder = findViewById(R.id.air_shoulder);
        mIvAirArm = findViewById(R.id.air_arm);
        mIvAirSeat = findViewById(R.id.air_seat);
        mIvAirCalf = findViewById(R.id.air_calf);
        mIvAirAnkle = findViewById(R.id.air_ankle);
        mIvAirFoot = findViewById(R.id.air_foot);
        mIvHotBack = findViewById(R.id.hot_back);
        mIvHotCalf = findViewById(R.id.hot_calf);
        mIvKnead = findViewById(R.id.knead);
        mIvZhiya = findViewById(R.id.zhiya);
        mIvPaida = findViewById(R.id.paida);
        mIvMassage = findViewById(R.id.massage);
        mIvRDS = findViewById(R.id.swedish);
        mIvKnock = findViewById(R.id.knock);
        mAnimatorImage.clear();


        mAnimatorImage.add(mIvKnead);//揉捏
        mAnimatorImage.add(mIvZhiya);//指压
        mAnimatorImage.add(mIvRDS);//瑞典
        mAnimatorImage.add(mIvMassage);//拍打
        mAnimatorImage.add(mIvPaida);//推拿
        mAnimatorImage.add(mIvKnock);//敲击
        addPointViews();
        initBody();
    }


    public void onDestory(){

       stopAnimation();

    }

    public void showFootRoll(boolean isShow){
        if(isShow){
            mIvFootRollTwo.setVisibility(VISIBLE);
            mIvFootRollOne.setVisibility(VISIBLE);
        }
        else {
            mIvFootRollTwo.setVisibility(INVISIBLE);
            mIvFootRollOne.setVisibility(INVISIBLE);
        }
    }
    /**
    *腰部加热
*/
    public void setHotBackShow(boolean isShow){
        if(isShow){
            mIvHotBack.setVisibility(VISIBLE);

        }
        else {
            mIvHotBack.setVisibility(GONE);

        }
    }

        /**
    *小腿加热
*/
    public void setHotCalfShow(boolean isShow){
        if(isShow){
            mIvHotCalf.setVisibility(VISIBLE);
        }
        else {
            mIvHotCalf.setVisibility(GONE);
        }
    }

    /**
     * 肩部气囊
     * @param isShow
     */
    public void setAirShoulderShow(boolean isShow){
        if(isShow){
            mIvAirShoulder.setVisibility(VISIBLE);
        }
        else {
            mIvAirShoulder.setVisibility(GONE);
        }
    }
    /**
     * 手部气囊
     * @param isShow
     */
    public void setAirHandShow(boolean isShow){
        if(isShow){
            mIvAirArm.setVisibility(VISIBLE);
        }
        else {
            mIvAirArm.setVisibility(GONE);
        }
    }
    /**
     * 腰部气囊
     * @param isShow
     */
    public void setAirSeatShow(boolean isShow){
        if(isShow){
            mIvAirSeat.setVisibility(VISIBLE);
        }
        else {
            mIvAirSeat.setVisibility(GONE);
        }
    }
    /**
     * 小腿气囊
     * @param isShow
     */
    public void setAirCalfShow(boolean isShow){
        if(isShow){
            mIvAirCalf.setVisibility(VISIBLE);
        }
        else {
            mIvAirCalf.setVisibility(GONE);
        }
    }

    /**
     * 脚部气囊
     * @param isShow
     */
    public void setAirFootShow(boolean isShow){
        if(isShow){
            mIvAirAnkle.setVisibility(VISIBLE);
            mIvAirFoot.setVisibility(VISIBLE);
        }
        else {
            mIvAirAnkle.setVisibility(GONE);
            mIvAirFoot.setVisibility(GONE);
        }
    }

    public void startkead() {
        CommonUtil.startFlick(mAnimatorImage.get(0));

    }
    public void stopkead() {
        CommonUtil.stopFlick(mAnimatorImage.get(0));

    }

    // 开始动画 小图标从上往下0 -- 5；
    public void setAnimatorPosition(int position) {
            for(int i = 1;i<mAnimatorImage.size();i++){
                if(position == i){
                    CommonUtil.startFlick(mAnimatorImage.get(i));
                }else {
                    CommonUtil.stopFlick(mAnimatorImage.get(i));
                }

            }

    }
    private void stopAnimation(){
        //停止所有动画
        for(int i = 0;i<mAnimatorImage.size();i++){
            CommonUtil.stopFlick(mAnimatorImage.get(i));
        }
    }

    private void initBody(){
        Device7808StateBean device7808StateBean = DataManager.getInst().getDevice7808StateBean();
        setHotCalfShow("1".equals(device7808StateBean.getKneeHeat()));
        setHotBackShow("1".equals(device7808StateBean.getHeatFullBody()));
        showFootRoll("1".equals(device7808StateBean.getFootWheelIcon()));
        setAirShoulderShow("1".equals(device7808StateBean.getAirCellShoulderStatus()));
        setAirSeatShow("1".equals(device7808StateBean.getAirCellSeatStatus()));
        setAirHandShow("1".equals(device7808StateBean.getAirCellHandStatus()));
        setAirCalfShow("1".equals(device7808StateBean.getAirCelllegStatus()));
        setAirFootShow("1".equals(device7808StateBean.getAirCellFootStatus()));
        int value = StringUtils.getValue(device7808StateBean.getYPos());
        showPoints(value);
//        if(!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusPointPosition))){
//            showPoints(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusPointPosition)));
//        }

        //小图标动画
        if ( StringUtils.getValue(device7808StateBean.getAutoProgram())>0) {
            startkead();
        } else {
            stopkead();
        }

        if(DataManager.getInst().isHandProgram()){
            setAnimatorPosition(getHandIcon());
        }
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == View.VISIBLE){
            if (!EventBus.getDefault().isRegistered(this)){
                EventBus.getDefault().register(this);
            }
        }else {
            if (EventBus.getDefault().isRegistered(this)){
                EventBus.getDefault().unregister(this);
            }

        }
    }
    @Subscribe
    public void receiveMassageDeviceChange(String message){
        if (!MqttConstant.EVENT_BUS_KEY_RECEIVE_MESSAGE.equals(message)){
            return;
        }
        try{
            initBody();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //    @Override
//    public void onRceiveSocket(SocketModel model) {
//        String value = model.getValue();
//        String key = model.getKey();
//        //小腿加热
//        if(model.getKey().equals(CommmandNum.mStatusHotLeg)){
//            setHotCalfShow(model.getValue().equals("1"));
//        }
//        //腰部加热
//        else if(model.getKey().equals(CommmandNum.mStatusHotYao)){
//            setHotBackShow(model.getValue().equals("1"));
//        }
//        //显示脚底滚轮
//        else if(model.getKey().equals(CommmandNum.mStatusJdglIcon)){
//            showFootRoll(model.getValue().equals("1"));
//        }
//        //肩部气囊
//        else if(model.getKey().equals(CommmandNum.mStatusAirShouldericon)){
//            setAirShoulderShow(model.getValue().equals("1"));
//        }
//        //腰部气囊
//        else if(model.getKey().equals(CommmandNum.mStatusAirSiticon)){
//            setAirSeatShow(model.getValue().equals("1"));
//        }
//        //手部气囊
//        else if(model.getKey().equals(CommmandNum.mStatusAirHandicon)){
//            setAirHandShow(model.getValue().equals("1"));
//        }
//        //小腿气囊
//        else if(model.getKey().equals(CommmandNum.mStatusAirLegicon)){
//            setAirCalfShow(model.getValue().equals("1"));
//        }
//        //脚部气囊
//        else if(model.getKey().equals(CommmandNum.mStatusAirFooticon)){
//            setAirFootShow(model.getValue().equals("1"));
//        }
//        //机芯点位
//        else if(model.getKey().equals(CommmandNum.mStatusPointPosition)){
//            showPoints(StringUtils.getValue(model.getValue()));
//        }
//        //小图标动画
//        else if(model.getKey().equals(CommmandNum.mStatusKneadIcon)){
//            if(!model.getValue().equals("0")){
//                startkead();
//            }else {
//                stopkead();
//            }
//        }
//        //小图标动画
//        else if(model.getKey().equals(CommmandNum.mStatusHandIcon)){
//
//           setAnimatorPosition(getHandIcon());
//        }
//
//    }



    public int getHandIcon(){
        Device7808StateBean deviceStatusBean =
                DataManager.getInst().getDevice7808StateBean();
        int hand = 0;
        if(StringUtils.getValue(deviceStatusBean.getShiasuStatus())>0){
            hand =1;
        }else if(StringUtils.getValue(deviceStatusBean.getSwedishStatus())>0){
            hand =2;
        }
        else if(StringUtils.getValue(deviceStatusBean.getFlapStatus())>0){
            hand =3;
        }
        else if(StringUtils.getValue(deviceStatusBean.getTuinaStatus())>0){
            hand =4;
        }
        else if(StringUtils.getValue(deviceStatusBean.getKnockStatus())>0){
            hand =5;
        }
        return hand;

    }


    private void addPointViews(){
        ImageView mPoint1 = findViewById(R.id.body_point1);
        ImageView mPoint2 = findViewById(R.id.body_point2);
        ImageView mPoint3 = findViewById(R.id.body_point3);
        ImageView mPoint4 = findViewById(R.id.body_point4);
        ImageView mPoint5 = findViewById(R.id.body_point5);
        ImageView mPoint6 = findViewById(R.id.body_point6);
        ImageView mPoint7 = findViewById(R.id.body_point7);
        ImageView mPoint8 = findViewById(R.id.body_point8);
        ImageView mPoint9 = findViewById(R.id.body_point9);
        ImageView mPoint10 = findViewById(R.id.body_point10);
        ImageView mPoint11 = findViewById(R.id.body_point11);


        mPointViews.add(mPoint11);
        mPointViews.add(mPoint10);
        mPointViews.add(mPoint9);
        mPointViews.add(mPoint8);
        mPointViews.add(mPoint7);
        mPointViews.add(mPoint6);
        mPointViews.add(mPoint5);
        mPointViews.add(mPoint4);
        mPointViews.add(mPoint3);
        mPointViews.add(mPoint2);
        mPointViews.add(mPoint1);
    }
    private void showPoints(int position){
        for(int i = 0;i<mPointViews.size();i++){
            mPointViews.get(position).setVisibility(VISIBLE);
//            if(position== i){
//                mPointViews.get(position).setVisibility(VISIBLE);
//            }
//            else {
//                mPointViews.get(i).setVisibility(GONE);
//            }
        }
    }
}
